# -*- test-case-name: activist.service -*-

from activist.DataStore import DataStore
from activist.Config import Config
from activist.XMLFactory import XMLFactory
import activist.Time

from twisted.trial import unittest
import time


class test_load(unittest.TestCase):

#    def setUp(self):
#        global journal
#        journal.sync(DataStore)

    def testDataStoreSerialization(self):
        print "\n\n\nload"
        import activist.DataStore
        import activist.Data
        activist.Data.next_id = 0
        activist.DataStore.DataStore = activist.DataStore._DataStore()
        activist.DataStore.DataStore.createGenericData()
        //activist.DataStore.DataStore.load()
        CommandProcessor.loadAndProcess()

        activist.DataStore.DataStore.dumpObjects()

#     def testDataStoreSerialization(self):
#         print "\n\n\ndump"
#         from activist.CommandProcessor import CommandProcessor
#         CommandProcessor.checkpoint()

#         print "\n\n\nload"
#         import activist.DataStore
#         activist.DataStore.DataStore = activist.DataStore._DataStore()
#         activist.DataStore.DataStore.load()
#         CommandProcessor.loadAndProcess()

    def testPersoninfoSerialization(self):
        from activist.Person import Person
        import pickle

        person = Person('foo bar', '765-4321', 'bar@foo.com', '123 Fake Street')

        if 0:
            repr = pickle.dumps(person)
            print "dumped person to:",repr
            personinfo = pickle.loads(repr).info()
        else:
            repr = person.info().getXML()
            print "dumped person to:",repr
            personinfo = XMLFactory.create(repr)
        
        self.failUnless( person.info()._name == personinfo._name )
        self.failUnless( person.info()._address == personinfo._address )
        for x in person.info()._phone:
            self.failUnless( x in personinfo._phone )
        for x in person.info()._email:
            self.failUnless( x in personinfo._email )

    def testDataCreation(self):
        from activist.Person import Person
        from activist.Group import Group
        from activist.Place import Place
        from activist.Event import Event
        from activist.EventInformation import EventInformation
        from activist.MetaData import MetaData
        from activist.GenericData import GenericData
        from activist.DataStore import DataStore
        from activist.Comment import Comment

        highlight = GenericData('highlighted events')
        moderated = GenericData('moderated')

        foo = DataStore.getGenericDataByTitle('highlighted events')
        self.failUnless(foo==highlight)

        personA = Person('mike warren', '123-4567', 'foo@bar.com', None)
        personB = Person('some one', '123-5555', 'nothing@nowhere.com', '#4 1492 64th Street NW\nCalgary, Alberta')

        try:
            personA.info()._name = 'foo'
        except:
            pass
        self.failUnless(personA.info()._name == 'mike warren')
        
        placeA = Place( 'Some Place', '456 Foo Bar Avenue', ['555-1234','555-4321'] )
        placeB = Place( 'A Location', '#1 1492 64th Street NW\nCalgary, Alberta', ['555-1234','555-4321'] )

        eventA = Event( personA, 'Event!', activist.Time.now(), 'Some random event, happening right now!', placeA )
        eventA._test()
        eventB = Event( personB, 'Some Other Event!', activist.Time.now(), 'Another event...', placeB )

        commentB = Comment( eventB, 'This event rocked!', 'I had a lot of fun at this event; everyone should check it out next time!', personA )
        
        personB = Person('foo bar', '765-4321', 'bar@foo.com', '123 Fake Street')

        groupA = Group( 'Fooity Bar', 'A random group of non-people.', "http://example.com",
                        [personA,personB], None, None, None, None )

        MetaDataFactory(eventA,MetaData.BelongsWith,groupA)

        self.failUnless( groupA in eventA.targetsForRelationship(MetaData.BelongsWith) )
        self.failUnless( personA in groupA.sourcesForRelationship(MetaData.BelongsWith) )
        self.failUnless( personB in groupA.sourcesForRelationship(MetaData.BelongsWith) )

        self.failUnless( isinstance(eventA.sourcesForRelationship(MetaData.InformationOf)[0],EventInformation) )
        self.failUnless( eventA.sourcesForRelationship(MetaData.InformationOf)[0] is eventA.info() )

#     def testService(self):
#         from activist.Data import Data
#         service = Service(os.path.join(Config.dataPath,'objects'))
#         #testdata = Data()
#         self.failUnlessEqual(service.loadObject(None,'99914b932bd37a50b983c5e7c90ae93b'),DataStore['99914b932bd37a50b983c5e7c90ae93b'])
#         import time
#         time.sleep(1.5)


